% vim: set ft=erlang:
begin
    TakeOutProjPlugin
        = fun (Name, Config) ->
            {project_plugins, ProjPlugins}
                = lists:keyfind(project_plugins, 1, Config),
            {value, {_, Version}, RemainingProjPlugins}
                = lists:keytake(Name, 1, ProjPlugins),
            logger:info("Dropping proj plugin '~ts' ~ts", [Name, Version]),
            _UpdatedConfig
                = lists:keystore(project_plugins, 1, Config,
                                 {project_plugins, RemainingProjPlugins})
          end,

    TakeOutElvis
        = fun (Config) ->
            TakeOutProjPlugin(rebar3_lint, Config)
          end,

    TakeOutHank
        = fun (Config) ->
            TakeOutProjPlugin(rebar3_hank, Config)
          end,

    TakeOutHankIfMaybeExprNotEnabledOnEnv
        = fun (Config) ->
            Env = os:getenv(),

            case [V || "ERL_FLAGS=" ++ V <- Env] of
                [Flags | _] ->
                    % katana-code (a dependent of rebar3_hank) requires maybe_expr
                    case string:find(Flags, "-enable-feature maybe_expr") of
                        nomatch ->
                            TakeOutHank(Config);
                        _ ->
                            Config
                    end;
                [] ->
                    TakeOutHank(Config)
            end
        end,


    OtpRelease = erlang:system_info(otp_release),
    try list_to_integer(OtpRelease) of
        TooOld when TooOld =< 22 ->
            % Take dev helpers away as they're no longer compatible with OTP 22
            TakeOutHank(TakeOutElvis(CONFIG));
        25 ->
            TakeOutHankIfMaybeExprNotEnabledOnEnv(CONFIG);
        _ ->
            CONFIG
    catch
        error:badarg ->
            logger:warning("Don't now how to compare to OTP release: ~tp", [OtpRelease]),
            CONFIG
    end
end.
